package com.baizhi.service.impl;

import com.baizhi.annotation.AddCacheAnnotation;
import com.baizhi.annotation.DeleteCacheAnnotation;
import com.baizhi.dao.GuruDao;
import com.baizhi.entity.Guru;
import com.baizhi.service.GuruService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class GuruServiceImpl implements GuruService {

    @Autowired
    private GuruDao guruDao;


    //分页查询上师信息
    @Override
    @AddCacheAnnotation
    public Map selectAllGuru(int page, int limit, String guruNickName) {
       /* if(guruNickName!=null && !guruNickName.equals("")){
         guruNickName="%"+guruNickName+"%";
        }else{
            guruNickName=null;
        }*/
       if(guruNickName==null||guruNickName.equals("")){
           guruNickName=null;
       }else {
           guruNickName="%"+guruNickName+"%";
       }
        int offset=(page-1)*limit;
        List<Guru> list=guruDao.selectAllGuru(offset,limit,guruNickName);
        int count=guruDao.selectCount(guruNickName);
        Map map=new HashMap();
        map.put("data",list);
        map.put("count",count);
        map.put("code",0);
        map.put("msg","");
        return map;
    }

    //添加上师信息
    @Override
    @DeleteCacheAnnotation
    public Map insertGuru(Guru guru) {
        int count =guruDao.insertGuru(guru);
        Map map=new HashMap();
        if(count>0){
            map.put("isAdd",true);
        }else{
            map.put("isAdd",false);
        }
        return map;
    }

    //更改上师状态
    @Override
    @DeleteCacheAnnotation
    public Map updateGuruStatus(Guru guru) {
        if(guru.getGuruStatus()==2){
            guru.setGuruStatus(1);
        }else{
            guru.setGuruStatus(2);
        }

        Map map=new HashMap();
        int count=guruDao.updateGuruStatus(guru);
        int a=guru.getGuruStatus();
        if(count>0&&a>0){
            map.put("isDelete",true);
        }else{
            map.put("isDelete",false);
        }
        return map;
    }

    //根据id查询上师信息
    @Override
    @AddCacheAnnotation
    public Guru selectByGuruId(int guruId) {


        return guruDao.selectByGuruId(guruId);
    }

    //修改上师信息
    @Override
    @DeleteCacheAnnotation
    public Map updateGuru(Guru guru) {
        Map map=new HashMap();
        int count=guruDao.updateGuru(guru);
        if(count>0){
            map.put("isUpdate",true);
        }else{
            map.put("isUpdate",false);
        }
        return map;

    }

    //修改没有图片的上师信息
    @Override
    public Map updateGuru1(Guru guru) {
        Map map=new HashMap();
        int count=guruDao.updateGuru1(guru);
        if(count>0){
            map.put("isUpdate",true);
        }else{
            map.put("isUpdate",false);
        }
        return map;
    }

    @Override
    public Map deleteGuru(int guruId) {
        int count =guruDao.deleteGuru(guruId);
        Map map=new HashMap();
        if(count>0){
            map.put("isDelete","删除成功");
        }else{
            map.put("isDelete","删除失败");
        }
        return map;
    }


}
